package com.szhmxxkj.config.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse.Record;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse.Domain;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
import com.aliyuncs.exceptions.ClientException;
import com.szhmxxkj.common.tools.core.support.BeanKit;
import com.szhmxxkj.common.tools.core.support.HttpKit;
import com.szhmxxkj.config.service.DynamicDnsService;

import lombok.extern.slf4j.Slf4j;

@Service("dynamicDnsService")
@Slf4j
public class DynamicDnsServiceImpl implements DynamicDnsService {

	@Autowired
	private IAcsClient client;

	@Override
	public List<Record> describeDomainRecords(String domainName) {
		DescribeDomainRecordsRequest request = new DescribeDomainRecordsRequest();
		request.setDomainName(domainName);
		DescribeDomainRecordsResponse response;
		try {
			response = client.getAcsResponse(request);
			List<Record> domainRecords = response.getDomainRecords();
			domainRecords.forEach(e -> {
				log.info("获取的解析信息："+BeanKit.beanToMap(e));
			});
			return domainRecords;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse.Record> describeSubDomainRecords(String subDomain) {
		DescribeSubDomainRecordsRequest request = new DescribeSubDomainRecordsRequest();
		DescribeSubDomainRecordsResponse response;
		request.setSubDomain(subDomain);
		try {
			response = client.getAcsResponse(request);
			List<com.aliyuncs.alidns.model.v20150109.DescribeSubDomainRecordsResponse.Record> domainRecords = response
					.getDomainRecords();
			domainRecords.forEach(e -> {
				System.out.println(BeanKit.beanToMap(e));
			});
			// {RR=gdgl, recordId=4031772651639808, line=default, domainName=szhmxxkj.com,
			return domainRecords;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public void describeDomains() {
		DescribeDomainsRequest request = new DescribeDomainsRequest();
		DescribeDomainsResponse response;
		try {
			response = client.getAcsResponse(request);
			List<Domain> list = response.getDomains();
			for (Domain domain : list) {
				System.out.println(domain.getDomainName());
			}
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateDomainRecord(String recordId, String RR) {
		UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
		request.setRecordId(recordId);
		request.setRR(RR);
		request.setType("A");
		String ip = getIp();
		request.setValue(ip);
		UpdateDomainRecordResponse response;
		try {
			log.info("开始更新{}的ip地址为{}",RR,ip);
			response = client.getAcsResponse(request);
		} catch (ClientException e) {
			log.error("根性记录失败", e);
		}
	}

	public static String getIp() {
		Map<String, String> param = new HashMap<String, String>();
		String sendGet = HttpKit.sendGet("http://myip.dnsomatic.com/", param);
		log.info("获取到ip：" + sendGet);
		return sendGet;
	}

	@Override
	public void updateDomainRecord(String recordId, String RR, String ip) {
		// TODO Auto-generated method stub
		UpdateDomainRecordRequest request = new UpdateDomainRecordRequest();
		request.setRecordId(recordId);
		request.setRR(RR);
		request.setType("A");
		request.setValue(ip);
		UpdateDomainRecordResponse response;
		try {
			log.info("开始更新{}的ip地址为{}",RR,ip);
			response = client.getAcsResponse(request);
		} catch (ClientException e) {
			log.error("根性记录失败", e);
		}
	}

}
